var love
export default class npcCtrl{
  constructor(_love,traffic = 1,chaos = 1,roadType = 5){
    love = love || _love
    this.traffic = traffic
    this.chaos = chaos
    this.type = roadType // 单向5车道
  }
  generateNPC(){
    var rnd = Math.random() //生成车辆
    if (rnd < this.traffic / 1000) {
      var rnd = Math.random()
      var npcType = 1 // 1 固定  2 单车道随机移动 3 试图占道 4 后方加速与玩家并排相撞 5 后方加速超过玩家后占道并减速
      if (rnd < this.chaos * 0.001) {
        love.makeNPC(-500, 5)
      } else if (rnd < this.chaos * 0.01) {
        love.makeNPC(-500, 4)
      } else if (rnd < this.chaos * 0.05) {
        love.makeNPC(2000, 3)
      } else if (rnd < this.chaos * 0.1) {
        love.makeItem(2000, 2)
      } else {
        love.makeNPC(2000, 1)
      }
    }
  }
  generateItem(){
    var rnd = Math.random() //生成车辆
    if (rnd < this.traffic / 1000) {
      love.makeItem(2000)
    }
  }
  checkCollNPC(){
    for (var i in love.npc) {
      let coll = love.car.checkColl(love.car,love.npc[i])
      if (coll) {
        love.system.vibrate("long")
        if (!love.car.unbreakable){
          love.car.hp = love.car.hp - 20
        }  
        love.npc[i].speed = love.car.speed
        if (!love.car.unstopable){
          love.car.speed = love.car.speed / 2
        }
        love.npc[i].throwAway(coll)
        love.particle.newParticle({
          x: coll.cx,
          y: coll.cy,
          life: 3000,
          dx: [-0.5, 0.5],
          dy: [-0.5, 0.5],
          da: -0.05,
          color: { r: 0, g: 0, b: 0, a: 1 }
        },10)
      }
    }
  }

  checkCollItem() {
    for (var i in love.items) {
      let item = love.items[i]
      let coll = item.checkColl(item,love.car)
      if (coll) {
        love.system.vibrate("long")
        item.effect()
        if (!item.floor){
          item.destroyed = true
        }
        love.particle.newParticle({
          x: coll.cx,
          y: coll.cy,
          life: 3000,
          dx: [-0.5, 0.5],
          dy: [-0.5, 0.5],
          da: -0.05,
          color: { r: 0, g: 0, b: 0, a: 1 }
        }, 10)
      }
    }
  }
  update(dt){
    this.generateNPC()
    this.generateItem()
    this.checkCollNPC()
    this.checkCollItem()
  }
}